package cn.wolfcode.luowowo.acticle.service.impl;


import cn.wolfcode.luowowo.acticle.domain.Strategy;
import cn.wolfcode.luowowo.acticle.domain.StrategyCatalog;
import cn.wolfcode.luowowo.acticle.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyMapper;
import cn.wolfcode.luowowo.acticle.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.acticle.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.common.exception.DisplayableException;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Service
@Transactional
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public PageInfo query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        return new PageInfo(strategyCatalogMapper.selectForList(qo));
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) throws DisplayableException {

        //冗余字段：目的地
        Strategy strategy = strategyMapper.selectByPrimaryKey(strategyCatalog.getStrategy().getId());
        strategyCatalog.setDestId(strategy.getDest().getId());

        if(strategyCatalog.getId() == null){
            strategyCatalogMapper.insert(strategyCatalog);
        }else{
            strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
        }

    }

    @Override
    public void delete(Long id) throws DisplayableException {
        strategyCatalogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<StrategyCatalog> list() {
        return strategyCatalogMapper.selectAll();
    }

    @Override
    public List<StrategyCatalog> getCatalogByStrategyId(Long strategyId) {
        return strategyCatalogMapper.selectCatalogByStrategyId(strategyId);
    }

    @Override
    public List<StrategyCatalog> queryByDestId(Long id) {
        StrategyCatalogQuery qo = new StrategyCatalogQuery();
        qo.setDestId(id);
        PageHelper.startPage(qo.getCurrentPage(), 0);
        return strategyCatalogMapper.selectForList(qo);
    }

    @Override
    public StrategyCatalog get(Long id) {
        return strategyCatalogMapper.selectByPrimaryKey(id);
    }
}
